package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxbbc.common.core.service.IWWarehouseService;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.basics.exp.ExpQueryUtil;
import com.sxbbc.common.basics.exp.ExpVo;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.OrderExpDetail;
import com.sxbbc.common.core.enums.ReceivedTypeEnums;
import com.sxbbc.common.core.mapper.jz.OrderExpDetailMapper;
import com.sxbbc.common.core.service.IOrderExpDetailService;

/**
 * <p>
 * 订单收货发货单表 服务实现类
 * </p>
 *
 * @author xiaoky
 * @since 2019-05-10
 */
@Service
public class OrderExpDetailServiceImpl extends ServiceImpl<OrderExpDetailMapper, OrderExpDetail> implements IOrderExpDetailService {

	@Autowired
	private IWWarehouseService iwWarehouseService;
	
	@Override
	public OrderExpDetail getByOrderCode(String orderCode) {
		QueryWrapper<OrderExpDetail> wrapper = new QueryWrapper<OrderExpDetail>();
		wrapper.eq("order_code", orderCode);
		OrderExpDetail expDetail = super.getOne(wrapper);
		if (expDetail != null) {
			handleLiveExpData(expDetail.getId());
		}
		return expDetail;
	}

	@Override
	public String handleLiveExpData(Integer expId) {
		String expMsg = "";
		OrderExpDetail expDetail = super.getById(expId);
		if (expDetail == null) {
			return expMsg;
		}
		expMsg = expDetail.getExpMsg();
		// 物流信息——如果物流未签收并且是物流发货
		if (CalcuUtils.getInstance().isEquals(expDetail.getSign(), StaticUtils.STATUS_NO) && CalcuUtils
				.getInstance().isEquals(expDetail.getReceivedType(), ReceivedTypeEnums.EXP.getId())) {
			String expCode = expDetail.getExpCode();
			String expNo = expDetail.getExpNo();
			String customerPhone = expDetail.getPhone();
			// 通过第三方接口获取物流信息
			ExpVo expVo = ExpQueryUtil.queryAliExpByCode(expCode, expNo,customerPhone);
			// 查询成功
			if (expVo.getQueryStatus()) {
				expDetail.setSign(expVo.getSign());
				expMsg = expVo.getExpDetail();
				expDetail.setExpMsg(expMsg);
				expDetail.setExpPhone(expVo.getExpPhone());
				// 更新物流信息
				super.updateById(expDetail);
			}
		}
		return expMsg;
	}

	@Override
	public IPage<OrderExpDetail> queryOrderExpList(BasePage<OrderExpDetail> page, OrderExpDetail expDetail) {
		if (StringUtils.isNotBlank(expDetail.getQueryTime())) {
			String[] times = expDetail.getQueryTime().split("~");
			expDetail.setStart(times[0].trim());
			expDetail.setEnd(times[1].trim());
		}
		baseMapper.queryOrderExpList(page,expDetail);
		return page;
	}

	private QueryWrapper<OrderExpDetail> queryWrapper(OrderExpDetail expDetail) {
		QueryWrapper<OrderExpDetail> wrapper = new QueryWrapper<>();
		if (StringUtils.isNotBlank(expDetail.getOrderCode())) {
			wrapper.eq("order_code",expDetail.getOrderCode());
		}
		return wrapper;
	}
}
